package com.cainiao.service.deptserviceimpl;

import com.cainiao.exception.BuinessException;
import com.cainiao.mapper.DeptMapper;
import com.cainiao.mapper.EmpMapper;
import com.cainiao.pojo.Dept;
import com.cainiao.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    public List<Dept> findDept() {
        return deptMapper.findDept();
    }

    @Override
    public void delete(Integer id) {
        Integer count=empMapper.countByDeptId(id);
        if(count>0){
            throw new BuinessException("该部门下还有员工，不能删除");
        }
        deptMapper.delete(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept findById(Integer id) {
        return deptMapper.findById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
